/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service;

import entity.Product;
import entity.ProductFacadeLocal;
import entity.SaleOrder;
import entity.SaleOrderDetail;
import entity.SaleOrderDetailFacadeLocal;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;

/**
 *
 * @author SONY
 */
@Stateless
public class SaleOrderDetailService implements SaleOrderDetailServiceLocal {
    
    @EJB SaleOrderDetailFacadeLocal saleOrderDetailFacadeLocal;
    @EJB ProductFacadeLocal productFacadeLocal;

    @Override
    public List<SaleOrderDetail> getAllSaleOrderDetails_bySaleOrderID(int saleorderid) {
        List<SaleOrderDetail> lstSaleOrderDetails_bysaleorderid = new ArrayList<SaleOrderDetail>();
        try {
            List<SaleOrderDetail> lstSaleOrderDetails = saleOrderDetailFacadeLocal.findAll();
            for(int i = 0; i < lstSaleOrderDetails.size(); i++) {
                SaleOrder sale_order = lstSaleOrderDetails.get(i).getSaleOrderID();
                if(sale_order.getSaleOrderID() == saleorderid) {
                    SaleOrderDetail sale_order_detail = lstSaleOrderDetails.get(i);
                    lstSaleOrderDetails_bysaleorderid.add(sale_order_detail);
                }
            }          
        } catch (Exception e) {
            System.err.println(e);
        }
        return lstSaleOrderDetails_bysaleorderid;
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public boolean addSaleOrderDetail(SaleOrder SaleOrderID, String ProductID, int OrderQty, BigDecimal LineTotal) {
        try {
            //add SaleOrderDetail info into SaleOrderDetail table
            SaleOrderDetail saleorderdetail = new SaleOrderDetail();
            saleorderdetail.setSaleOrderDetailID(1);
            saleorderdetail.setSaleOrderID(SaleOrderID);
            saleorderdetail.setOrderQty(OrderQty);
            saleorderdetail.setLineTotal(LineTotal);
            saleorderdetail.setProductID(productFacadeLocal.find(ProductID));
            saleOrderDetailFacadeLocal.create(saleorderdetail);
            return true;
        } catch (Exception e) {
            System.err.println(e);
        }
        return false;
    }

    @Override
    public Product exist_Product(String pid) {
        Product p = null;
        try {
            List<SaleOrderDetail> lstSaleOrderDetail = saleOrderDetailFacadeLocal.findAll();
            for(int i = 0; i < lstSaleOrderDetail.size(); i++) {
                if(lstSaleOrderDetail.get(i).getProductID().getProductID().equals(pid)) {
                    p = lstSaleOrderDetail.get(i).getProductID();
                    break;
                }
            }
        } catch (Exception e) {
            System.err.println(e);
        }
        return p;
    }


}
